# coding:utf-8
# name ：YuFuXiao
# date : 2018-9-XX
class BookViewModel():
    '''
    数据要求
    'title':'',
    'publisher':'',
    'pages':'',
    'author':'',
    'price':0,
    'summary':'',
    'image':''
    '''

    @classmethod
    def package_single(cls, data, keyword):
        returned = {
            'total': 0,
            'book': [],
            'keyword': keyword,
        }
        if data:
            returned['total'] = 1
            returned['book'].append(cls.handle_book_data(data))
        return returned

    @classmethod
    def package_collection(cls, data, keyword):
        returned = {
            'total': data['total'],
            'books': [],
            'keyword': keyword,
        }
        if data:
            returned['books'] = [cls.handle_book_data(book_item) for book_item in data['books']]
        return returned

    @classmethod
    def handle_book_data(cls, data):
        book = {
            'title': data['title'],
            'publisher': data['publisher'],
            'pages': data['pages'],
            'author': '、'.join(data['author']),
            'price': data['price'],
            'summary': data['summary'],
            'image': data['image'],
            'isbn': data.get('isbn13') or data.get('isbn10')
        }
        book['intro'] = cls.info(book)
        return book

    # 金庸/三联书店/96.0
    # 金庸/96.0
    @staticmethod
    def info(book):
        intro = filter(lambda x: True if x else False,
                       [book.get('author'), book.get('publisher'), book.get('price')])

        return ' / '.join(intro)
